package com.ruibang.glass.quality.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruibang.glass.common.constants.RedisKey;
import com.ruibang.glass.quality.domain.req.QualityWeighReq;
import com.ruibang.glass.quality.entity.QualityWeigh;
import com.ruibang.glass.quality.mapper.QualityWeighMapper;
import com.ruibang.glass.quality.service.QualityWeighService;
import com.teaming.cloud.framework2.autoconfigure.redis.RedisSingleService;
import com.teaming.cloud.framework2.autoconfigure.redis.RedisSingleService;
import com.teaming.cloud.framework2.common.constant.CommonConstant;
import com.teaming.cloud.framework2.common.pojo.PageRequest;
import com.teaming.cloud.framework2.common.pojo.PageResult;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 半成品称重信息 服务实现类
 * </p>
 *
 * @author wujie
 * @since 2024-03-11
 */
@Service
public class QualityWeighServiceImpl extends ServiceImpl<QualityWeighMapper, QualityWeigh> implements QualityWeighService {

//    @Autowired
//    private RedisSingleService redisService;

    @Autowired
    private RedisSingleService redisService;

    @Override
    public void saveInfo(QualityWeigh qualityWeigh) {
        LocalDateTime now = LocalDateTime.now();
        if(qualityWeigh.getTransectTime()==null){
            qualityWeigh.setTransectTime(now);
        }
        if(qualityWeigh.getRecordTime()==null){
            qualityWeigh.setRecordTime(now);
        }
        this.save(qualityWeigh);
    }

    @Override
    public void updateInfo(QualityWeigh qualityWeigh) {
        //更新文件信息
        this.updateById(qualityWeigh);
    }

    @Override
    public void delete(String id) {
        lambdaUpdate().eq(QualityWeigh::getWeighId, id).set(QualityWeigh::getIsDelete, CommonConstant.ONE_STR).update();
    }


    @Override
    public PageResult<QualityWeigh> queryPage(PageRequest<QualityWeighReq> pageRequest) {
        final QualityWeighReq param = pageRequest.getParam();
        final LambdaQueryWrapper<QualityWeigh> lambda = new QueryWrapper<QualityWeigh>().lambda();
        this.buildCondition(lambda,param);
        final IPage<QualityWeigh> page = this.page(new Page<>(pageRequest.getPageNo(), pageRequest.getPageSize()), lambda);
        if(page!=null&&CollectionUtils.isNotEmpty(page.getRecords())){
            Map<Object, Object> userMap = redisService.hmget(RedisKey.USER_GROUP_TYPE);
            for (QualityWeigh record : page.getRecords()) {
                if(userMap!=null){
                    record.setUserName(userMap.get(record.getUserId())==null?"":userMap.get(record.getUserId()).toString());
                }
            }
        }

        PageResult<QualityWeigh> pr = new PageResult(page);
        return pr;
    }

    @Override
    public List<QualityWeigh> queryQualityWeigh(QualityWeighReq weighReq) {
        final LambdaQueryWrapper<QualityWeigh> lambda = new QueryWrapper<QualityWeigh>().lambda();
        this.buildCondition(lambda,weighReq);
        return this.list(lambda);
    }
    
    /**
     * 构造查询条件
     * @param lambda
     * @param param
     */
    private void buildCondition(LambdaQueryWrapper<QualityWeigh> lambda, QualityWeighReq param){
        lambda.eq(QualityWeigh::getIsDelete, CommonConstant.ZERO_STR);
        if(null == param) return;
        if(StringUtils.isNotBlank(param.getWeighId())){
            lambda.eq(QualityWeigh::getWeighId, param.getWeighId());
        }
        if(StringUtils.isNotBlank(param.getProductLine())){
            lambda.eq(QualityWeigh::getProductLine, param.getProductLine());
        }
        if(StringUtils.isNotBlank(param.getGlassId())){
            lambda.eq(QualityWeigh::getGlassId, param.getGlassId());
        }
        if(StringUtils.isNotBlank(param.getGlassType())){
            lambda.eq(QualityWeigh::getGlassType, param.getGlassType());
        }
        if(StringUtils.isNotBlank(param.getGlassModel())){
            lambda.eq(QualityWeigh::getGlassModel, param.getGlassModel());
        }
        if(StringUtils.isNotBlank(param.getUserId())){
            lambda.eq(QualityWeigh::getUserId, param.getUserId());
        }
        if(param.getRecordStartTime()!=null&&param.getRecordEndTime()!=null){
            lambda.ge(QualityWeigh::getRecordTime, param.getRecordStartTime());
            lambda.le(QualityWeigh::getRecordTime, param.getRecordEndTime());
        }
        if(param.getTransectStartTime()!=null&&param.getTransectEndTime()!=null){
            lambda.ge(QualityWeigh::getTransectTime, param.getTransectStartTime());
            lambda.le(QualityWeigh::getTransectTime, param.getTransectEndTime());
        }
        if(StringUtils.isNotBlank(param.getTeam())){
            lambda.eq(QualityWeigh::getTeam, param.getTeam());
        }
        lambda.orderBy(true,false, QualityWeigh::getTransectTime);
    }

}
